package com.example.listviewdemo;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private int[] imgIds = {R.drawable.tiger, R.drawable.nongyu, R.drawable.qingzhao, R.drawable.libai};
    private String[] names;
    private String[] descs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listView = findViewById(R.id.list);
//
//        TextView txtView=new TextView(this);
//        txtView.setText("----课程列表----");
//        txtView.setTextSize(25);
//        listView.addHeaderView(txtView);
//        listView.addFooterView(new View(this));
//        String arr[]=getResources().getStringArray(R.array.books);
////        ArrayAdapter<String> aa=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked,arr);
////        listView.setAdapter(aa);
        final String[] names = getResources().getStringArray(R.array.names);
        String[] descs = getResources().getStringArray(R.array.descs);

        List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < names.length; i++) {
            Map<String, Object> item = new HashMap<String, Object>();
            item.put("head_image", imgIds[i]);
            item.put("name", names[i]);
            item.put("desc", descs[i]);
            data.add(item);
        }
        SimpleAdapter sa = new SimpleAdapter(this, data, R.layout.list_item,
                new String[]{"head_image", "name", "desc"},
                new int[]{R.id.head_image, R.id.name, R.id.description});
        listView.setAdapter(sa);

//        BaseAdapter ba=new BaseAdapter() {
//            @Override
//            public int getCount() {
//                return names.length;
//            }
//
//            @Override
//            public Object getItem(int i) {
//                return null;
//            }
//
//            @Override
//            public long getItemId(int i) {
//                return 0;
//            }
//
//            @Override
//            public View getView(int i, View view, ViewGroup viewGroup) {
//                if (view!=null)
//                    return view;
//                LinearLayout linearLayout=new LinearLayout(MainActivity.this);
//                linearLayout.setOrientation(LinearLayout.HORIZONTAL);
//                linearLayout.setGravity(Gravity.CENTER_VERTICAL);
//                ImageView headImg=new ImageView(MainActivity.this);
//                headImg.setLayoutParams(new ViewGroup.LayoutParams(120,120));
//                headImg.setScaleType(ImageView.ScaleType.FIT_CENTER);
//                headImg.setImageResource(imgIds[i]);
//                headImg.setPadding(10,20,20,20);
//                linearLayout.addView(headImg);
//                TextView nameTxt=new TextView(MainActivity.this);
//                nameTxt.setTextSize(20);
//                nameTxt.setText(names[i]);
//                linearLayout.addView(nameTxt);
//                return linearLayout;
//            }
//        };
//        listView.setAdapter(ba);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(MainActivity.this, "你要跟" + names[i] + "聊天", Toast.LENGTH_LONG).show();
            }
        });
    }
}